<template>
  <div class="login-container">
    <img
      src="@/assets/images/top_login/login_10.png"
      class="login_login"
      alt=""
    />
    <img
      src="@/assets/images/top_login/login_3.png"
      style="width: 100%; height: 100%; object-fit: cover"
      alt=""
    />
    <div class="login-border_box flex-y flex-center">
      <div class="flex-x flex-center" style="width: 50%">
        <div
          class="w100 flex-x flex-center"
          style="position: relative; margin-bottom: 10px"
        >
          <!-- <img
            src="@/assets/images/top_login/login_title.png"
            style="width: 100%"
            alt=""
          /> -->

          <!-- <img
            class="login_title_icon"
            src="@/assets/images/top_login/login_12.png"
            alt=""
          /> -->
          <div class="login_title_text">{{ title }}</div>
        </div>
      </div>

      <div class="login-border">
        <div
          class="flex-x flex-center w100"
          style="margin-top: 60px; margin-bottom: 4px"
        >
          <img
            class="login-border_title_icon"
            src="@/assets/images/top_login/login_8.png"
            style="width: 15px; height: 15px"
            alt=""
          />
          <div class="login-border_title">欢迎登陆</div>
          <img
            class="login-border_title_icon"
            src="@/assets/images/top_login/login_8.png"
            style="width: 15px; height: 15px"
            alt=""
          />
        </div>
        <img
          src="@/assets/images/top_login/login_9.png"
          style="width: 100%"
          alt=""
        />
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form_box"
          auto-complete="on"
          label-position="left"
        >
          <el-form-item prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            >
              <i
                slot="prefix"
                style="color: #00eaff; font-size: 15px"
                class="el-icon-user"
              ></i>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              placeholder="Password"
              show-password
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            >
              <i
                slot="prefix"
                style="color: #00eaff; font-size: 15px"
                class="el-icon-unlock"
              ></i>
            </el-input>
          </el-form-item>
          <div style="min-height: 30px">
            <div class="err_msg_box flex-x" v-if="errorMsg">
              <span class="el-icon-remove" style="color: #d0300f"></span>
              <div>{{ errorMsg }}</div>
            </div>
          </div>
          <el-button
            :loading="loading"
            type="primary"
            style="width: 100%; margin-bottom: 30px"
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </el-form>
      </div>
      <img
        style="width: 750px"
        src="@/assets/images/top_login/login_7.png"
        alt=""
      />
    </div>
    <div class="copyright">版权所有：金航数码科技有限责任公司</div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import _ from "lodash";
import { title } from "@/settings";
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请填写用户名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error("请填写密码"));
      } else {
        callback();
      }
    };
    return {
      errorMsg: "",
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        username: [
          // { required: true, trigger: "blur", validator: validateUsername },
          { required: true, trigger: "blur", message: "请输入用户名" }
        ],
        password: [
          // { required: true, trigger: "blur", validator: validatePassword },
          { required: true, trigger: "blur", message: "请输入密码" }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      title
    };
  },
  mounted() {
    // 回显上一次登录的账号密码
    if (this.$store.state.user.usernamePassword) {
      this.loginForm = _.cloneDeep(this.$store.state.user.usernamePassword);
      // console.log("this.loginForm :>> ", this.loginForm);
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.errorMsg = "";
          this.loading = true;
          this.$store
            .dispatch("user/getTenantList", this.loginForm)
            .then(res => {
              this.$store
                .dispatch("user/login", res)
                .then(async () => {
                  this.loading = false;
                  // location.reload();
                  this.$router.replace({
                    path: "/"
                  });
                  // this.$router.go(-1);
                })
                .catch(() => {
                  this.loading = false;
                });
            })
            .catch(error => {
              if (error) {
                this.errorMsg = error.message;
              }

              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  /* background-image: url("../../assets/images/top_login/login_3.png");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center; */
  position: relative;
  .copyright {
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 34px;
    background-color: rgba(0, 0, 0, 0.4);
    text-align: center;
    line-height: 38px;
    font-size: 14px;
    color: #fff;
  }
  .login_title_text {
    background: linear-gradient(0deg, #6b95eb 0%, #fff 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    color: #ffffff;
    font-size: 34px;
    white-space: nowrap;

    font-family: MicrosoftYaHei;

    font-weight: bold;
  }
  .login_title_icon {
    /* position: absolute;
    left: -44px;
    top: 50%; */
    width: 40px;
    /* transform: translate(0, -50%); */
    margin-right: 10px;
  }
  .login_login {
    position: absolute;
    top: 20px;
    left: 20px;
    /* width: 400px; */
  }
  .err_msg_box {
    background: #ceb2b1;
    border: 1px solid #d0300f;
    border-radius: 10px;
    padding: 10px;
    margin-bottom: 20px;
    div {
      margin-left: 6px;
      font-size: 13px;
      color: #1c1a1a;
    }
  }
  .login-form_box {
    width: 70%;
    margin: 20px auto 0 auto;
    .el-button {
      background: linear-gradient(0deg, #ff6802, #ffc01d) !important;
      border: none !important;
    }
    .el-input__icon {
      color: #00eaff;
      font-size: 15px;
    }
    .el-input__inner {
      background-color: transparent;
      box-shadow: 0px 0px 0px 0px #5ffffa, inset 0px 0px 15px 0.3px #0197ea;
      border: 1px solid #00eaff;
      border-radius: 10px;
    }
  }
  .login-border_box {
    position: fixed;
    top: 50%;
    right: 0;
    transform: translate(0, -50%);
    margin-top: 60px;
  }
  .login-border {
    background: url("../../assets/images/top_login/login_6.png") no-repeat;
    background-size: 100% 100%;
    width: 50%;
    height: 380px;
    .login-border_title {
      color: #ffffff;
      font-size: 20px;
      font-weight: 600;
      margin: 0 10px;
    }
    .login-border_title_icon {
    }
  }
}
</style>
